package Test;

import Beans.Course;
import Beans.Selection;
import Beans.Student;
import mapper.demo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;

@Component
public class Test {
    private demo Demo;
    private SqlSession sqlSession;
    @BeforeEach
    public void init()throws Exception{
        String res = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(res);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
        Demo = sqlSession.getMapper(demo.class);
    }
    @org.junit.jupiter.api.Test
    public void Studnet()
    {
       Student stu = new Student("123455","小明","1");
        Student stu1 = new Student("123456","小红","1");
       Demo.StuInsert(stu); //添加学生
       Demo.StuInsert(stu1);//添加学生
       Demo.stuUpdate("123455");//修改学生
       Student stu3 = Demo.findStuById("123455");//查询学生
       System.out.println("id:"+stu3.getId()+'\n'+"姓名:"+stu3.getName()+'\n'+"年级:"+stu3.getGrade());
       sqlSession.commit();
       sqlSession.close();
    }
    @org.junit.jupiter.api.Test
    public void courseTest()
    {
        Course cou1 = new Course("001","JavaEE2","3.5");
        Course cou2 = new Course("002","软件需求","3.5");
        Course cou3 = new Course("003","软件测试","3.5");
        Demo.InsertCourse(cou1);//插入课程
        Demo.InsertCourse(cou2);//插入课程
        Demo.InsertCourse(cou3);//插入课程
        Demo.CourseUpdate("003");//修改课程学分
        List<Course> list = Demo.findAllCourse();
        for(Course co:list)
        {
            System.out.println(co.getTitle()+" "+co.getCredits());
        }
        sqlSession.commit();
        sqlSession.close();
    }
    @org.junit.jupiter.api.Test
    public void SelectionTest()
    {
        Demo.InsertSelection("123455","001","2019-09-01",0);//添加选课
        Demo.InsertSelection("123455","002","2019-09-01",1);
        Demo.InsertSelection("123455","003","2019-09-01",2);
        Demo.InsertSelection("123456","003","2019-09-01",3);
        Demo.DeleteSelection(1);
        List<Selection> list = Demo.findAllSelection();
        for(Selection s:list)
        {
            System.out.println(s.getStudentId()+" "+s.getCourseId()+" "+s.getCtime());
        }
       sqlSession.commit();
        sqlSession.close();
    }
}
